﻿
{% extends "cmdb/mequipment/index.html" %}

{% block right_content %}

<div id="right_menu">
    <b class="modal-title">服务器添加</b>
    <hr/>
    <form class="form-horizontal">

        <div style="margin-bottom: 20px;border-bottom: 2px #eee solid;"><span id="machine" style="cursor:pointer">设备信息</span><span id="system" style="cursor:pointer">系统信息</span></div>
        <div id="machine_info">
        <!-- <div class="form-inline" style="padding-left:40px"> -->

        <div class="column-row">
        <!-- <div class="form-inline"> -->
           <div class="column-text ">
                <label class="position_center" for="equipmentType" >设备类型</label>
           </div>
           <div class="column-body-single-input-text">
            <select class="form-control" id="equipmentType" style="" >
                {% for idx,single_type in equipment_type_list.items %}
                <option value="{{ idx }}" {% ifequal equipment_info.equipment_type idx %} selected {% endifequal %}>{{ single_type }}</option>
                {% endfor %}
            </select>
            </div>
        </div>
        <br>

        <div class="column-row">
            <div class="column-text ">
                <label for="seriesNo" class="position_center">设备序列号<span style="color:#ff0000; ">*</span></label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="seriesNo" placeholder="设备序列号" value="{{ equipment_info.serial_num }}" />
                <span style="display:inline" id="warningSerialNo"></span> 
            </div>
        </div>
        <br>

        <div class="column-row">
            <div class="column-text">
                <label class="position_center" for="assetNumber">资产编号</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="assetNumber" placeholder="资产编号" />
                <span style="display:none">*必填</span>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                <label class="position_center" for="equipmentModel">设备型号</label>
            </div>
            <div class="column-body-single-input-text">
                 <input class="form-control input_form_control" type="text" id="equipmentModel" placeholder="设备型号" />
            </div>
        </div><br>

         <div class="column-row">
            <div class="column-text">
<label for="equipmentModel" class="position_center">cpu型号</label>
            </div>
            <div class="column-body-single-input-text">
 <input class="form-control input_form_control" type="text" id="cpuModel" placeholder="cpu型号" value="{{ equipment_info.cpu_model }}" />
            </div>
        </div><br>
       
        <div class="column-row">
            <div class="column-text">
                <label class="position_center" for="hostname">主机名<span style="color:#ff0000;">*</span></label>
            </div>
            <div class="column-body-single-input-text">
                 <input class="form-control" style="width:400px;" type="text" id="hostname" placeholder="主机名" value="{{ equipment_info.hostname }}" />
            <span style="display: inline" id="warningHostName"></span>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                <label for="innerip">内网IP<span style="color:#ff0000;">*</span></label>
            </div>
            <div class="column-body-single-input-text">
                 <input class="form-control" type="text" id="innerip" placeholder="内网IP" value="{{ equipment_info.sys_ip_inner }}" onchange="ipUniqueCheck()" />
            <span style="display: inline" id="warningIp"></span>
        </div>
        </div><br>

        <div class="column-row">

            <div class="column-text ">
                <label style="float: left">物理地址:</label>
            </div>

            <div class="column-body-mutiple-input-text">

                <div class="first_input_text ">

                    <input class="form-control"  type="text" id="card0" placeholder="网卡名" value="{{ equipment_info.network_card_first.card_name }}"/>

                </div>

                <div class="second_input_text ">

                    <div class="input_2_text ">
                        <input class="form-control" type="text" id="mac0" placeholder="MAC地址" value="{{ equipment_info.network_card_first.mac }}" />
                    </div>

                    <div class="img_plus ">
                        <img src="{{ STATIC_URL }}img/add.png" class="plus-image" onclick="addPhysical()">
                    </div>
                </div>
                <!--<input class="form-control " style="width: 40%;" type="text" id="card0" placeholder="网卡名" value="{{ equipment_info.network_card_first.card_name }}"/>-->
                <!--<input class="form-control box_border" style="float: right;width: 40%" type="text" id="mac0" placeholder="MAC地址" value="{{ equipment_info.network_card_first.mac }}" />-->
                <!--<img src="{{ STATIC_URL }}img/add.png" class="plus-image" onclick="addPhysical()">-->
            </div>

        </div>

        <div class="row">
            <div style="padding-left:59px">
                <table id="physicalAddr" style="position: relative; left: 57px;">
                    {% if equipment_info.network_card_list %}
                    {% for idx,single_card in equipment_info.network_card_list.items %}
                    <tr id="physical_tr{{ idx }}">
                        <td><input class="form-control" style="width:100%;" type="text" id="card{{ idx }}" value="{{ single_card.card_name }}" /></td>
                                <td><input class="form-control" style="width:220px;" type="text" id="mac{{ idx }}" value="{{ single_card.mac }}" /></td>
                        <td><img src="/static/img/delete.png" onclick="delPhsical(this)" /></td>
                    </tr>
                    {% endfor %}
                    {% endif %}
                </table>
            </div>
         </div><br>

        <div class="column-row">
            <div class="column-text">
                <label class="position_center" for="outerip">外网IP</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="outerip" placeholder="外网IP" />
                <span style="display:none;">*必填</span>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                 <label class="position_center" for="cpu">CPU</label>
            </div>
            <div class="column-body-single-input-text">
                  <input class="form-control" type="text" id="cpu" placeholder="CPU"  value="{{ equipment_info.cpu }}" />
                  <span style="display:none;">*必填</span>
            </div>
        </div><br>
   
        <div class="column-row">
            <div class="column-text">
                <label for="memory" class="position_center">内存</label> 
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="memory" placeholder="内存" value="{{ equipment_info.memory }}"/>
                <span style="display:none;">*必填</span>
            </div>
        </div><br>

         <div class="column-row">
            <div class="column-text">
                <label for="harddisk" class="position_center">硬盘</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="harddisk" placeholder="硬盘" />
                <span style="display:none;">*必填</span>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                <label for="environment" class="position_center">使用环境</label>
            </div>
            <div class="column-body-single-input-text">
                 <select class="form-control" id="environment" style="width:400px">
                    {% for idx,single_environment in environment_list.items %}
                        <option value="{{ idx }}">{{ single_environment }}</option>
                    {% endfor %}
                </select>
            </div>
        </div><br>
 
        <div class="column-row">
            <div class="column-text">
                <label for="serverRoom" class="position_center">机房</label>
            </div>

            <div class="column-body-mutiple-input-text">

                <div class="first_input_text ">
                    {% if first_room_info %}
                    <select class="form-control" id="serverRoom" onchange="serverRoomChange()" style="">
                        <option value="{{ first_room_info.id }}">{{ first_room_info.server_room }}</option>
                        {% if server_room_list %}
                        {% for id,serverRoom in server_room_list.items %}
                        {% ifnotequal id first_room_info.id %}
                        <option value="{{ id }}">{{ serverRoom.server_room }}</option>
                        {% endifnotequal %}
                        {% endfor %}
                        {% endif %}
                    </select>
                    {% endif %}
                </div>

                <div class="second_input_text ">
                    <select class="form-control" id="roomNo" >
                            {% for single_room_no in first_room_info.room_no_info %}
                            <option value="{{ single_room_no.id }}">{{ single_room_no.room_no }}</option>
                            {% endfor %}
                    </select>
                </div>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                <label for="cabinet" class="position_center">机柜</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="cabinet" placeholder="机柜"/>
            </div>
        </div><br>

         <div class="column-row">
            <div class="column-text">
                 <label for="iDRAC" class="position_center">管理地址</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="iDRAC" placeholder="iDRAC"/>
            </div>
        </div><br>

         <div class="column-row">
            <div class="column-text">
                 <label for="uStartPos" class="position_center">机柜起位</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="uStartPos" placeholder="机柜起位" onblur="occupyUCompute()"/>
            </div>
        </div><br>

          <div class="column-row">
            <div class="column-text">
                 <label for="uEndPos" class="position_center">机柜止位</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="uEndPos" placeholder="机柜止位" onblur="occupyUCompute()"/>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                 <label for="occupyU" class="position_center">占用U数</label>
            </div>
            <div class="column-body-single-input-text">
                <input class="form-control" type="text" id="occupyU" placeholder="占用U数"  disabled/>
         
            </div>
        </div><br>
       
       <div class="column-row">
                <div class="column-text">
                     <label for="startTime" class="position_center">采购日期</label>
                </div>
                <div class="column-body-single-input-text">
                    <input type="text" id="startTime" class="form-control input" readonly><span style="display:none;">*必填</span>
                </div>
        </div><br>
             
        <div class="column-row">
            <div class="column-text">
                <label for="maintEndTime" class="position_center">维保到期</label>
            </div>
            <div class="column-body-single-input-text">
                <input type="text" id="maintEndTime" class="form-control input" readonly>
                <span style="display:none;">*必填</span>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                <label for="manufacturerId" class="position_center">制造商</label>
            </div>
            <div class="column-body-single-input-text">

                <select class="form-control" id="manufacturerId" style="width:400px">
                   {% if merchant_info %}
                    {% for merchant in merchant_info %}
                    <option value="{{ merchant.id }}">{{ merchant.factory_name }}</option>
                    {% endfor %}
                   {% endif %}
                </select>

            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                 <label for="providerId" class="position_center">供应商</label>
            </div>
            <div class="column-body-single-input-text">
                 <select class="form-control" id="providerId" style="width:400px">
                    {% if merchant_info %}
                     {% for merchant in merchant_info %}
                        <option value="{{ merchant.id }}">{{ merchant.factory_name }}</option>
                     {% endfor %}
                    {% endif %}
                </select>
            </div>
        </div><br>


        <div class="column-row">
            <div class="column-text">
                  <label for="comment1" class="position_center">设备负责人</label>
            </div>
            <div class="column-body-single-input-text">
                  <input class="form-control" type="text" id="responser" placeholder="设备负责人" />
                <span style="display:none;">*必填</span>
            </div>
        </div><br>

        <div class="column-row">
            <div class="column-text">
                  <label for="comment1" class="position_center">备注</label>
            </div>
            <div class="column-body-single-input-text">
                 <input class="form-control" type="text" id="comment1" placeholder="备注" />
              <span style="display:none;">*必填</span>
            </div>
        </div><br>
        
        <input type="text" id="operator" value="{{ request.session.username }}" style="display: none;">
        </div>

        <div id="system_info" style="display: none;">
            <div class="column-row">
                <div class="column-text">
                    <label for="sysVersion" class="position_center">系统版本</label>
                </div>
                <div class="column-body-single-input-text">
                     <input class="form-control" type="text" id="sysVersion" placeholder="系统版本" value="{{ equipment_info.system_ver }}" />
                    <span style="display:none;">*必填</span>
                </div>
            </div><br>

            <div class="column-row">
                <div class="column-text">
                    <label for="coreVersion" style="vertical-align: top;" class="position_center">内核版本</label>
                </div>
                <div class="column-body-single-input-text">
                     <input class="form-control" type="text" id="coreVersion" placeholder="内核版本" value="{{ equipment_info.core_ver }}" />
                </div>
            </div><br>

            <div class="column-row">
                <div class="column-text">
                    <label for="parentIp" style="vertical-align: top;" class="position_center">系统父ip</label>
                    
                </div>
                <div class="column-body-single-input-text">
                   <input class="form-control" type="text" id="parentIp" placeholder="系统父ip" />
                </div>
            </div><br>

            <div class="column-row">

                <div class="column-text">

                   <label for="useStatus" style="vertical-align: top;" class="position_center">使用状态</label>
                    
                </div>
                <div class="column-body-single-input-text">

                    <select class="form-control" id="useStatus" style="width:400px">
                    {% for key,single_u in use_status_list.items %}
                        <option value="{{ key }}">{{ single_u }}</option>
                    {% endfor %}
                    </select>

                </div>
            </div><br>

            <div class="column-row">

                <div class="column-text">
                   <label for="belongSys0" style="vertical-align: top;" class="position_center">所属系统
                   </label>
                </div>

                <div class="column-body-mutiple-input-text ">

                    <div class="one-input ">
                        <input class="form-control" type="text" id="belongSys0" placeholder="所属系统" disabled/>
                    </div>

                    <div class="one-plus ">
                        <!--<img src="{{ STATIC_URL }}img/add.png" onclick="addByScript()" class="" >-->
                    </div>

                </div>

            </div><br>

            <div class="row">
                <div style="padding-left:59px;">
                    <table id="belongSys" class="belong_sys_table">
                    </table>
                </div>
            </div>

            <div class="column-row">

                <div class="column-text">
                    <label for="comment2" class="position_center">备注</label> 
                </div>

                <div class="column-body-single-input-text">
                    <input class="form-control" type="text" id="comment2" placeholder="备注" />
                    <span style="display:none;">*必填</span>
                </div>
            </div><br>

        </div>
    </form>
    
    <div style="padding-left: 100px;" >
        <button type="button" class="btn btn-primary " id="item_add">
            提交
        </button>&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default" id="edit_cancel">
            取消
        </button>
    </div>

</div>

<script type="text/javascript">
    var myDate1 = new Calender({id:'startTime',isSelect:!0});
    var myDate2 = new Calender({id:'maintEndTime',isSelect:!0});
    $("#serverRoom").val({{ first_room_info.id }});
    function serverRoomChange(){
        var roomId = $("#serverRoom").val();
        jQuery("#roomNo").empty();
        {% for id,serverRoom in server_room_list.items %}
            var tmpId = {{ id }};
            if(tmpId == roomId){
                {% for single_room_no in serverRoom.room_no_info %}
                    jQuery("#roomNo").append('<option value="{{ single_room_no.id }}">{{ single_room_no.room_no }}</option>');
                {% endfor %}
            }
        {% endfor %}
    }
    function occupyUCompute(){
        var uStartPos = $("#uStartPos").val();
        if(uStartPos == ''){
            return;
        }
        uStartPos = (uStartPos == '') ? '0' : uStartPos;
        var uEndPos = $("#uEndPos").val();
        if(uEndPos == ''){
            return;
        }
        uEndPos = (uEndPos == '') ? '0' : uEndPos;
        value = Math.abs(Number(uEndPos) - Number(uStartPos));
        $("#occupyU").val(value);
    }

    $("#add").click(function(){
        window.location.href="{% url 'equipment:equipmentadd' %}";
    });

    var count = 1;
    function addByScript() {
        var table = document.getElementById("belongSys");
        var newRow = table.insertRow(table.rows.length);
        newRow.id = "row" + count;
        var belongSysId = "belongSys" + count;
        var contentCell = newRow.insertCell(-1);
        contentCell.innerHTML = '<input class="form-control" style="width:400px;" type="text" id="'+belongSysId+'"/>';
        contentCell = newRow.insertCell(-1);
        var delBtn = document.createElement("img");
        delBtn.src = "{{ STATIC_URL }}img/delete.png";
        delBtn.id = "btnDel"+count;
        delBtn.onclick = new Function("del(this)");
        contentCell.appendChild(delBtn);
        count++;
    }
    function del(obj) {
        var row = obj.parentNode.parentNode;
        row.parentNode.removeChild(row);
    }

    var physicalCount = {{ physical_count }};
    function addPhysical(){
        var physicalId = "physical_tr" + physicalCount;
        var cardId = "card" + physicalCount;
        var macId = "mac" + physicalCount;
        $("#physicalAddr").append('<tr id="' + physicalId + '">'
                                  + '<td style="width: 125px;"><input class="form-control" style="width:120px; margin-left: 3px;" type="text" id="' + cardId + '" placeholder="网卡名" /></td>'
                                  + '<td style="width: 280px;"><input class="form-control" style="width: 260px; position: relative; left: 18px;" type="text" id="' + macId + '" placeholder="MAC地址" /></td>'
                                  + '<td style="width:20px; "><img style="position: relative; left: 7px;" src="/static/img/delete.png" onclick="delPhsical(this)"/></td></tr>');
        physicalCount++;
    }
    function delPhsical(obj) {
        var row = obj.parentNode.parentNode;
        row.parentNode.removeChild(row);
    }
    $("#edit_cancel").click(function(){
        window.location.href="{% url 'equipment:home' %}";
    });

    function uniqueCheck(value,attr){
        if(value == ''){
             $("#" + attr).html("<font size='2' style='color:red'>不能为空</font>");
			return false;
		}
        $("#" + attr).html("");
        var checkStatus = true;
        var urls = "{% url 'equipment:equniquecheck' %}";
        var postdata = {}
        if(attr == 'warningIp'){
            var postdata = {sys_ip_inner:value};
            var jsonstr = $.ajax({
			    url : urls,
			    type : "POST",
			    data : postdata,
			    async : false,
			    cache : false
			});
	        var jsonobj = eval('(' + jsonstr.responseText + ')');
		    if(jsonobj.status == 1){
			    if(jsonobj.data == true){
			        $("#" + attr).html("<font size='2' style='color:red'>设备已存在</font>");
			        checkStatus = false;
			    }else{
			        $("#" + attr).html("");
			    }
		    }else{
		        checkStatus = false;
		    }
		}
		return checkStatus;
    }

    //系统ip唯一性判断
    function ipUniqueCheck(){
        var innerip = $("#innerip").val();
        uniqueCheck(innerip,'warningIp');
    }

    $("#item_add").click(function(){
        var eType = $("#equipmentType").val();
        var equipmentModel = $("#equipmentModel").val();
        var cpuModel = $("#cpuModel").val();
        var seriesNo = $("#seriesNo").val();
        var hostname = $("#hostname").val();
        var innerip = $("#innerip").val();
        if(!uniqueCheck(seriesNo,'warningSerialNo') || !uniqueCheck(hostname,'warningHostName') || !uniqueCheck(innerip,'warningIp')){
            return;
        }
        var card = $("#card0").val();
        var mac = $("#mac0").val();
        var netCardArr = new Array();
        if(card!=''){
            var tmpCardStr = 'card_name#' + card + ';mac#' + mac;
            netCardArr.push(tmpCardStr);
        }
        for(var i=0; i < $("#physicalAddr").children().find('tr').length; i++){
            var trIdStr = $("#physicalAddr").children().find('tr').eq(i).attr('id');
            var trId =  trIdStr.substr(11);
            var cardName = $("#card" + trId).val();
            var mac = $("#mac" + trId).val();
            var tmpMacStr = 'card_name#' + cardName + ';mac#' + mac;
            netCardArr.push(tmpMacStr);
        }
        var outerip = $("#outerip").val();

        var assetNumber = $("#assetNumber").val();
        var cpu = $("#cpu").val();
        var memory = $("#memory").val();
        var harddisk = $("#harddisk").val();
        var environment = $("#environment").val();
        var serverRoom = $("#serverRoom").val();
        if(!serverRoom || serverRoom == ''){
            alert('机房选择为空值，请先配置机房信息');
            return;
        }
        var roomNo = $("#roomNo").val();
        if(!roomNo || roomNo == ''){
            roomNo = 0;
        }
        var cabinet = $("#cabinet").val();
        var idrac = $("#iDRAC").val();
        var uStartPos = $("#uStartPos").val();
        uStartPos = (uStartPos == '') ? '0' : uStartPos;
        var uEndPos = $("#uEndPos").val();
        uEndPos = (uEndPos == '') ? '0' : uEndPos;
        var machinePos = $("#machinePos").val();
        var startTime = $("#startTime").val();
        var maintEndTime = $("#maintEndTime").val();
        var manufacturerId = $("#manufacturerId").val();
        if(!manufacturerId || manufacturerId==''){
            alert('制造商选择为空，请先配置商家信息');
            return;
        }
        var providerId = $("#providerId").val();
        if(!providerId || providerId==''){
            alert('供应商选择为空，请先配置商家信息');
            return;
        }
        var comment1 = $("#comment1").val();
        var responser = $("#responser").val();

        var operator = $("#operator").val();
        var sysVersion = $("#sysVersion").val();
        var coreVersion = $("#coreVersion").val();
        var parentIp = $("#parentIp").val();
        var useStatus = $("#useStatus").val();

        var belongSysArr = new Array();
        var belongSysStr = ''
        var sysIdx = 0;
        var belongSys0 = $("#belongSys0").val();
        if(belongSys0 != ''){
            belongSysArr[sysIdx++] = belongSys0;
            belongSysStr = belongSys0
        }
        for(var i=0; i < $("#belongSys").children().find('input').length; i++){
            var tempBelongSys = $("#belongSys").children().find('input').eq(i).val();
            if(tempBelongSys != ''){
                if($.inArray(tempBelongSys,belongSysArr) == -1){
                    belongSysArr[sysIdx++] = tempBelongSys;
                    belongSysStr = (belongSysStr == '' ) ? tempBelongSys : belongSysStr + ',' + tempBelongSys;
                }
            }
        }

        var comment2 = $("#comment2").val();

        var urls = "{% url 'equipment:eqaddaction' %}";
        var postdata = {equipment_type:eType,equipment_model:equipmentModel,cpu_model:cpuModel,hostname:hostname,sys_ip_inner:innerip,network_card_info:JSON.stringify(netCardArr),
                    sys_ip_outer:outerip,serial_num:seriesNo,asset_number:assetNumber,cpu:cpu,memory:memory,harddisk:harddisk,environment:environment,
                    server_room_id:serverRoom,room_no_id:roomNo,cabinet:cabinet,idrac:idrac,start_cabinet:uStartPos,end_cabinet:uEndPos,machine_pos:machinePos,
                    start_time:startTime,maint_end_time:maintEndTime,manufacturer_id:manufacturerId,provider_id:providerId,responser:responser,
                    comment1:comment1,operator:operator,system_ver:sysVersion,core_ver:coreVersion,sys_parent_ip:parentIp,
                    use_status:useStatus,belong_sys_list:belongSysStr,comment2:comment2};
        var jsonstr = $.ajax({
			url : urls,
			type : "POST",
			data : postdata,
			async : false,
			cache : false
		});
		var jsonobj = eval('(' + jsonstr.responseText + ')');
		if(jsonobj.status==0){
		    alert(jsonobj.message);
		}else{
            window.location.href="{% url 'equipment:home' %}";
        }

    });

    $("#machine").click(function(){
        $(this).css("color","#e38d13");
        $(this).css("background","#eee");
        $("#system").css("background","none");
        $("#system").css("color","#333");
        $("#machine_info").show();
        $("#system_info").hide();
    });
    $("#system").click(function(){
        $(this).css("color","#e38d13");
        $(this).css("background","#eee");
        $(this).css("border","#eee");
        $("#machine").css("background","none");
        $("#machine").css("color","#333");
        $("#machine_info").hide();
        $("#system_info").show();
    });
</script>
{% endblock %}